#include "Loan.h"

/*
*	Constructors
*/
Loan::Loan(double loanAmount, int numberOfPayments, double annualInterestRate) {
	/*
	*	Initialize
	*/
	setLoanAmount(loanAmount);
	setNumberOfPayments(numberOfPayments);
	setAnnualInterestRate(annualInterestRate);
} //Loan(double loanAmount, int numberOfPayments, double annualInterestRate)

Loan::Loan() {
	/*
	*	Initialize
	*/
	setLoanAmount(0);
	setNumberOfPayments(0);
	setAnnualInterestRate(0);
} //Loan()

/*
*	Operators
*/
double	Loan::getLoanAmount()			{ return loanAmount;			}
int		Loan::getNumberOfPayments()		{ return numberOfPayments;		}
double	Loan::getAnnualInterestRate()	{ return annualInterestRate;	}

void Loan::setLoanAmount  (double loanAmount)	{
	if (loanAmount < 0)				exit(EXIT_CODE_ERROR);

	this->loanAmount = loanAmount;
} //setLoanAmount(double loanAmount)	
void Loan::setNumberOfPayments  (int numberOfPayments) {
	if (numberOfPayments < 0)		exit(EXIT_CODE_ERROR);

	this->numberOfPayments = numberOfPayments;
} //setNumberOfPayments (int numberOfPayments)
	
void Loan::setAnnualInterestRate(double annualInterestRate) {
	if (annualInterestRate < 0)		exit(EXIT_CODE_ERROR);

	this->annualInterestRate = annualInterestRate;
} //setAnnualInterestRate(double annualInterestRate)

/*
*	Methods
*/
double Loan::calculateMonthlyInterestRate() {
	return getAnnualInterestRate()/12;
} //calculateMonthlyInterestRate()

double Loan::calculateMonthlyPayment() {
	double monthlyInterest	= calculateMonthlyInterestRate()/100;
	int numberOfPayments	= getNumberOfPayments();
	double amount			= getLoanAmount();
		
	/*
	*	Monthly payment = [ Amount * Monthly Interest * (1 + Monthly Interest)^number of payments ]  / [ (1 + Monthly Interest)^number of payments - 1 ]
	*/
	return (amount * monthlyInterest * pow(1 + monthlyInterest, numberOfPayments)) / (pow(1 + monthlyInterest, numberOfPayments) - 1);
} //calculateMonthlyPayment()

double Loan::calculateTotalAmountPaid()		{ return calculateMonthlyPayment()*getNumberOfPayments();	}
double Loan::calculateTotalInterest()		{ return calculateTotalAmountPaid() - getLoanAmount();		}